// pages/payment/index/index.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //基本参数
    NonceStr: '',
    timestamp: '',



  },
  //去详情
  go_detail(){
    let that=this;
    wx.navigateTo({
      url: '../detail/detail',
    })
  },
  // 付钱
  click_pay(e) {
    // console.log(e)
    let that = this;
    const encryptedData = e.detail.encryptedData;
    const iv = e.detail.iv;
    that.setData({
      encryptedData: encryptedData,
      iv: iv,
    })
    var timer = setTimeout(function() {
      wx.login({
        success(e) {
          // console.log(e.code);
          that.setData({
            code: e.code,
          })
          clearTimeout(timer)
          //原始支付方式
          // that.get_openId(that.data.code, that.data.encryptedData, that.data.iv)
          //新支付方式
          // var openId = wx.getStorageSync('openId');
          // that.pay_package(openId);
          if (wx.getStorageSync('openId')) {
            const openId = wx.getStorageSync('openId');
            that.pay_package(openId);
          } else {
            wx.showModal({
              content: '请先进行授权',
              showCancel: true,
              cancelText: '取消',
              cancelColor: '#333333',
              confirmText: '好的',
              confirmColor: '#d0a76c',
              success: (e) => {
                if (e.confirm) {
                  wx.navigateTo({
                    url: '../../author/author',
                  })
                }
              }
            })
          }
        }
      })
    }, 0)

  },
  //获取openId >> 
  get_openId(code, encryptedData, iv) {
    let that = this;
    wx.request({
      url: app.globalData.url + 'api/sign/login/getWxLogin',
      data: {
        code: code,
        encryptedData: encryptedData,
        iv: iv,
      },
      method: 'POST',
      success: function(e) {
        console.log(e)
        const getuserinfo = JSON.parse(e);
        let openId = getuserinfo.openId;
        that.pay_package(openId)
      },
    })
  },
  //提交后台获取
  pay_package(openId) {
    let that = this;
    wx.request({
      url: app.globalData.url + 'api/recruit/pay_package',
      data: {
        token: app.globalData.token,
        package_num: "3",
        price: "6",
        pay_type: "4",
        from: "3",
        openId: openId
      },
      method: 'POST',
      success: function(e) {
        // console.log(e)
        let _appId = e.data.data.appId,
          _nonceStr = e.data.data.nonceStr,
          _package = e.data.data.package,
          _paySign = e.data.data.paySign,
          _signType = e.data.data.signType,
          _timeStamp = e.data.data.timeStamp;
        wx.requestPayment({
          appId: _appId,
          nonceStr: _nonceStr,
          package: _package,
          paySign: _paySign,
          signType: _signType,
          timeStamp: _timeStamp,
          success(e){
            // console.log(e.errMsg)
            wx.showModal({
              content: '支付完成',
              showCancel: false,
              confirmText: '好',
              confirmColor: '#d0a76c',
              success: function(e) {
                console.log(e.confirm)
                if (e.confirm){
                  wx.navigateBack({
                    delta: 1
                  })
                }
              },
            })
          }
        })
      },
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})